package com.serviceprovider.business.module.admin.role.services.dao.impl;

//~--- non-JDK imports --------------------------------------------------------

import com.serviceprovider.business.module.admin.role.model.RoleHierarchy;
import com.serviceprovider.business.module.admin.role.services.dao.interfaces.IRoleHierarchyService;
import com.serviceprovider.business.module.common.Page;
import com.serviceprovider.business.module.common.PaginationHelper;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.jdbc.core.JdbcTemplate;
import org.springframework.jdbc.core.simple.ParameterizedRowMapper;
import org.springframework.jdbc.core.support.JdbcDaoSupport;
import org.springframework.stereotype.Service;

//~--- JDK imports ------------------------------------------------------------

import java.io.Serializable;

import java.sql.ResultSet;
import java.sql.SQLException;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

@Service("roleHierarchyService")
public class RoleHierarchyServiceImpl extends JdbcDaoSupport implements IRoleHierarchyService, Serializable {
    @Autowired
    public RoleHierarchyServiceImpl(@Qualifier("jdbcTemplate") JdbcTemplate jdbcTemplate) {
        setJdbcTemplate(jdbcTemplate);
    }

    @Override
    public void deleteById(int[] idList) {
        final String sql = "delete from role_hierarchy where id = ?";    // $NON-NLS-1$

        for (int i = 0; i < idList.length; i++) {
            getJdbcTemplate().update(sql, new Object[] { idList[i] });
        }
    }

    @Override
    public List<RoleHierarchy> findAll() {
        String              sql   = "select id,role1,sign,role2 from role_hierarchy";    // $NON-NLS-1$
        List                list  = getJdbcTemplate().queryForList(sql);
        List<RoleHierarchy> roles = new ArrayList<RoleHierarchy>();
        Map                 row   = null;
        RoleHierarchy       aux   = null;

        for (Object obj : list) {
            row = (Map) obj;
            aux = new RoleHierarchy();
            aux.setId(new Integer(row.get("id").toString()));    // $NON-NLS-1$
            aux.setRole1(new Integer(row.get("role1").toString()));
            aux.setRole2(new Integer(row.get("role2").toString()));
            aux.setSign(row.get("sign").toString());
            roles.add(aux);
        }

        return roles;
    }

    public Page<RoleHierarchy> findAll(int pageNo, int recordSize, String sortColumn, String sortOrder) {
        PaginationHelper<RoleHierarchy>       pageHelper = new PaginationHelper<RoleHierarchy>();
        ParameterizedRowMapper<RoleHierarchy> mapper     = new RoleHierarchyRowMapper();
        String                                sql        = "select id,role1,sign,role2 from role_hierarchy order by " + sortColumn + " " + sortOrder;    // $NON-NLS-1$
        String countSql = "SELECT COUNT(*) FROM role_hierarchy";

        return pageHelper.fetchPage(getJdbcTemplate(), countSql, sql, new Object[] {}, pageNo, recordSize, sortColumn,
                                    sortOrder, mapper);
    }

    @Override
    public RoleHierarchy getById(int id) {
        String        sql = "select id,role1,sign,role2 from role_hierarchy where id = " + id;    // $NON-NLS-1$
        Map           row = getJdbcTemplate().queryForMap(sql);
        RoleHierarchy aux = new RoleHierarchy();

        aux.setId(new Integer(row.get("id").toString()));    // $NON-NLS-1$
        aux.setRole1(new Integer(row.get("role1").toString()));
        aux.setRole2(new Integer(row.get("role2").toString()));
        aux.setSign(row.get("sign").toString());

        return aux;
    }

    @Override
    public void insertRoleHierarchy(RoleHierarchy r) {
        final String sql = "insert into role_hierarchy (role1,sign,role2) values(?,?,?)";    // $NON-NLS-1$

        getJdbcTemplate().update(sql, new Object[] { r.getRole1(), r.getSign(), r.getRole2() });
    }

    @Override
    public void update(RoleHierarchy r) {
        final String sql = "update role_hierarchy set role1 = ?, sign = ?, role2 = ? where id = ?";    // $NON-NLS-1$

        getJdbcTemplate().update(sql, new Object[] { r.getRole1(), r.getSign(), r.getRole2(), r.getId() });
    }

    private static class RoleHierarchyRowMapper implements ParameterizedRowMapper<RoleHierarchy> {
        @Override
        public RoleHierarchy mapRow(ResultSet rs, int index) throws SQLException {
            RoleHierarchy roleHierarchy = new RoleHierarchy();

            roleHierarchy.setId(rs.getInt("id"));    // $NON-NLS-1$
            roleHierarchy.setRole1(rs.getInt("role1"));
            roleHierarchy.setRole2(rs.getInt("role2"));
            roleHierarchy.setSign(rs.getString("sign"));

            return roleHierarchy;
        }
    }
}



